var t = require("../../../@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.dateAddZero = l;

var e = t(require("../../../@babel/runtime/regenerator")), a = require("../../../@babel/runtime/helpers/asyncToGenerator"), s = require("../../../@babel/runtime/helpers/toConsumableArray"), i = require("../../../@babel/runtime/helpers/objectSpread2"), o = require("../../../@babel/runtime/helpers/defineProperty"), n = getApp(), r = [], d = {};

function c(t, e) {
    return isNaN(Number(t)) && (t = new Date(t).getTime()), isNaN(Number(e)) && (e = new Date(e).getTime()), 
    a = (t - e) / 1e3, s = Math.floor(a % 60), i = Math.floor(a / 60) % 60, o = Math.floor(a / 60 / 60) % 24, 
    {
        days: Math.floor(a / 60 / 60 / 24),
        hours: o,
        minutes: i,
        seconds: s
    };
    var a, s, i, o;
}

function h() {
    var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : null, e = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "y-m-d h:i:s";
    null === t && (t = Date.now());
    var a = u(t), s = a.year, i = a.month, o = a.day, n = a.hour, r = a.minute, d = a.second, c = a.week, h = [ "一", "二", "三", "四", "五", "六", "日" ];
    return e = (e = (e = (e = (e = (e = (e = (e = e.replace(/y/gi, s)).replace(/m/gi, l(i))).replace(/d/gi, l(o))).replace(/h/gi, l(n))).replace(/i/gi, l(r))).replace(/s/gi, l(d))).replace(/w/g, c)).replace(/W/g, h[c - 1]);
}

function u(t) {
    null === t && (t = Date.now());
    var e = new Date(t);
    return {
        year: e.getFullYear(),
        month: l(e.getMonth() + 1),
        day: l(e.getDate()),
        hour: l(e.getHours()),
        minute: l(e.getMinutes()),
        second: l(e.getSeconds()),
        week: e.getDay()
    };
}

function l(t) {
    return (t = parseInt(t)) < 10 ? "0".concat(t) : t;
}

Page({
    data: {
        showPage: !1,
        URL: "",
        customerPopupShow: !1,
        cordId: "",
        wx_url: "",
        msgText: "",
        heartBeatInterval: null,
        msg_id_list: [],
        chatType: 1,
        employeeId: 0,
        groupId: 0,
        userUid: 0,
        queue_id: 0,
        emojiList: [],
        emojiObjs: {},
        chatList: [],
        emojisTmp: [],
        goodsList: [],
        goodsPopupShow: !1,
        goodsSearchKwd: "",
        orderList: [],
        orderPopupShow: !1,
        orderSearchKeyword: "",
        replyList: [],
        replyPopupShow: !1,
        replySearchKwd: "",
        transferEmployeeList: [],
        transferPopupShow: !1,
        transferSearchKwd: "",
        websock: null,
        timeout: 3e3,
        timeoutObj: null,
        serverTimeoutObj: null,
        timeoutnum: null,
        isClosed: !1,
        lockReconnect: !1,
        maxReconnectNum: 10,
        currentReconnectNum: 0,
        scrollTopTarget: "",
        scrollTopAnimation: !1,
        chatLoadedPage: 1,
        chatFinished: !1,
        chatGetLoading: !1,
        operation: {
            iptFocus: !1,
            flag: !1,
            height: 50,
            riseHeight: 220,
            dropHeight: 50,
            isRiseFlag: !1,
            safeBottom: 0,
            emotion: !1,
            iptCursor: 0
        },
        chat_opt_setting: {},
        goodsObj: {
            show: !1,
            info: {}
        },
        withdrawObj: {
            left: 0,
            top: 0,
            height: 30,
            width: 75,
            show: !1,
            item: {},
            direction: 0
        },
        plugin_name: "",
        message_type: "",
        show_tool_bar: 0,
        chat_tool_bars: [],
        disabledSend: !1
    },
    onLoad: function(t) {
        t.goods_id && 0 != t.goods_id && this.getGoodsDetails(t.goods_id), console.log(this.data.chatType), 
        console.log(t), this.setData({
            plugin_name: t.plugin_name || "",
            message_type: t.message_type || "",
            emojiList: r,
            emojiObjs: d,
            chatType: t.chatType,
            employeeId: t.employeeId,
            groupId: t.groupId,
            userUid: t.user_id,
            queue_id: t.queue_id || ""
        });
        var e = wx.getSystemInfoSync().screenHeight, a = wx.getSystemInfoSync().safeArea.bottom;
        this.setData({
            "operation.safeBottom": e - a
        }), this.init(), 2 == this.data.chatType && this.getReplyList(), this.getOrderList();
    },
    handClickChatToolBars: function(t) {
        var e = t.currentTarget.dataset.item;
        0 == e.type ? (this.data.msgText = e.key_word, this.sendMsg(0)) : 1 == e.type && wx.navigateTo({
            url: e.mini_app_link,
            fail: function(t) {
                n.tips("客服路由出错");
            }
        });
    },
    bindClickInput: function(t) {
        var e = this;
        console.log("点击", t);
        var a = this.data.operation;
        a.isRiseFlag && (console.log(a.iptFocus, a.flag), this.setData({
            "operation.iptFocus": !0,
            "operation.flag": !0,
            "operation.height": a.riseHeight + a.dropHeight
        }, function() {
            e.scrollBottom();
        }), console.log("点击"));
    },
    inputFocus: function(t) {
        console.log("获取光标", t);
        var e = t.detail.height, a = this.data.operation;
        a.isRiseFlag || (0 == e && (e = 220), e != a.riseHeight && this.setData({
            "operation.riseHeight": e
        }), this.setData({
            "goodsObj.show": !1,
            "operation.isRiseFlag": !0,
            "operation.iptFocus": !0,
            "operation.flag": !0,
            "operation.height": a.riseHeight + a.dropHeight
        }), this.scrollBottom(), console.log(t));
    },
    handLongpress: function(t) {
        var e = this, a = t.currentTarget.dataset, s = a.index, i = a.item, o = "#my-info-" + s;
        console.log(o), wx.createSelectorQuery().select(o).boundingClientRect(function(t) {
            console.log(t);
            var a = t.top, s = t.width, o = t.left, n = t.bottom, r = e.data.withdrawObj, d = o - (r.width - s) / 2, c = a - r.height - 15, h = 0;
            console.log(r.width - s), a < 80 && (h = 1, c = n + 15), e.setData({
                "withdrawObj.direction": h,
                "withdrawObj.left": d,
                "withdrawObj.top": c,
                "withdrawObj.show": !0,
                "withdrawObj.item": i
            });
        }).exec();
    },
    inputBlur: function(t) {
        var e = this;
        this.data.operation.iptCursor = t.detail.cursor, setTimeout(function() {
            console.log("我先执行2"), e.setData({
                "operation.iptFocus": !1
            });
        }, 300);
    },
    handClickAddIcon: function() {
        wx.hideKeyboard(), this.setOpetFlag();
    },
    setOpetFlag: function() {
        var t = this, e = this.data.operation;
        console.log("我先执行", e), 0 == e.flag ? this.setData({
            "goodsObj.show": !1,
            "operation.flag": !0,
            "operation.height": e.riseHeight + e.dropHeight
        }, function() {
            t.scrollBottom();
        }) : 0 == e.iptFocus && this.setData({
            "operation.flag": !1,
            "operation.height": e.dropHeight
        });
    },
    setEmotionFlag: function() {
        wx.hideKeyboard(), this.setData({
            "operation.emotion": !this.data.operation.emotion
        });
    },
    onReady: function() {},
    viewPicture: function(t) {
        var e = t.currentTarget.dataset.src, a = [];
        this.data.chatList.forEach(function(t) {
            1 == t.content_type && a.push(t.content);
        }), wx.previewImage({
            urls: a,
            current: e
        }), console.log(e, this.data.chatList);
    },
    floatDeFocus: function() {
        this.setData({
            "withdrawObj.show": !1,
            "operation.emotion": !1
        });
    },
    getGoodsDetails: function(t) {
        var e = this, a = n.getNetAddresss("plugin.yun-chat.frontend.goods.queryOne"), s = {
            id: t
        };
        Number(t) && n._postNetWork({
            url: a,
            data: s,
            success: function(t) {
                var a = t.data;
                1 === a.result && (e.setData({
                    "goodsObj.info": a.data,
                    "goodsObj.show": !0
                }), setTimeout(function() {
                    e.setData({
                        "goodsObj.show": !1
                    });
                }, 5e3));
            }
        });
    },
    bindSendGoods: function() {
        this.setData({
            "goodsObj.show": !1
        });
        var t = this.data.goodsObj.info, e = {};
        e.id = t.id, e.title = t.title, e.thumb = t.thumb, e.price = t.min_price || t.price, 
        console.log(JSON.stringify(e)), this.data.msgText = encodeURIComponent(JSON.stringify(e)), 
        this.sendMsg(2);
    },
    backMsg: function() {
        var t = this;
        this.floatDeFocus();
        var e = this.data.withdrawObj.item.id, a = "";
        1 == this.data.chatType ? a = n.getNetAddresss("plugin.yun-chat.frontend.h5.chat.backed-msg") : 2 == this.data.chatType && (a = n.getNetAddresss("plugin.yun-chat.frontend.chat.backed-msg")), 
        n._postNetWork({
            url: a,
            data: {
                id: e
            },
            success: function(a) {
                var s = a.data;
                if (1 != s.result) return n.tips(s.msg);
                for (var i = [], o = 0; o < t.data.chatList.length; o++) t.data.chatList[o].id == e && (t.data.chatList[o].is_backed = 1), 
                i.push(t.data.chatList[o]);
                t.setData({
                    chatList: i
                });
            }
        });
    },
    sendMsgBtn: function() {
        this.sendMsg(0);
    },
    sendMsg: function(t) {
        var e = this;
        if (this.data.disabledSend) console.log("没有客服或者该客服已停用"); else if (this.data.msgText) {
            var a = this.data.chatList[this.data.chatList.length - 1], s = !1;
            a && (s = !0);
            for (var r = this.data.msgText, d = 0; d < this.data.emojisTmp.length; d++) r = r.replace(this.data.emojisTmp[d].text, this.data.emojisTmp[d].name);
            var c, u = {
                text: r,
                content_type: t,
                queue_id: this.data.queue_id
            }, l = "";
            1 == this.data.chatType ? (c = this.generateChatData(r, 1, this.getNowDateTime(), t), 
            l = n.getNetAddresss("plugin.yun-chat.frontend.h5.chat.sendMsg")) : 2 == this.data.chatType && (c = this.generateChatData(r, 0, this.getNowDateTime(), t), 
            l = n.getNetAddresss("plugin.yun-chat.frontend.chat.sendMsg"));
            var p = this.data.chatList.length;
            this.setData(o({}, "chatList[" + p + "]", c)), this.scrollBottom(), n._postNetWork({
                url: l,
                data: u,
                success: function(t) {
                    var a = t.data;
                    if (1 === a.result) {
                        if (e.fmtChatList(a.data), e.setData(o({}, "chatList[" + p + "]", a.data)), s) {
                            var r = e.data.chatList;
                            r.splice(p, 0, {
                                _is_time: !0,
                                _message_time: h(Date.now())
                            }), e.setData({
                                chatList: r
                            });
                        }
                    } else e.data.chatList.push(i(i({}, c), {}, {
                        send_fail: 1
                    })), e.setData({
                        chatList: e.data.chatList
                    }), n.tips(a.msg);
                },
                fail: function() {
                    var t = "chatList[" + p + "].send_fail";
                    e.setData(o({}, t, 1));
                }
            }), this.setData({
                msgText: ""
            });
        }
    },
    generateChatData: function(t, e, a, s) {
        var i = {
            id: 0
        };
        return 2 == s || 3 == s ? i.content = JSON.parse(decodeURIComponent(t)) : 1 == s ? i.content = t : 0 == s && (i.content = this.replaceEmoji(t)), 
        i.content_type = s, i.direction_type = e, i.created_at = a, i;
    },
    replaceEmoji: function(t) {
        var e = this;
        return t.replace(/face\[([^\s\[\]]+?)\]/g, function(t) {
            return '<img src="' + e.data.emojiObjs[t].url + '" style="width:30px;height:30px;"/>';
        });
    },
    onShow: function() {},
    onHide: function() {
        this.clearHeartBeat();
    },
    onUnload: function() {
        this.clearHeartBeat(), this.data.isClosed = !0, this.data.websock && this.data.websock.close();
    },
    onPullDownRefresh: function() {},
    selectImage: function() {
        var t = this;
        wx.chooseImage({
            count: 9,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(e) {
                console.log(e.tempFilePaths), t.unload({
                    tempFilePaths: e.tempFilePaths
                });
            }
        });
    },
    unload: function(t) {
        var e = this;
        if (0 != t.tempFilePaths.length) {
            wx.showLoading({
                title: "上传中"
            });
            var a = n.getNetAddresss("upload.uploadPic");
            wx.uploadFile({
                url: a,
                filePath: t.tempFilePaths[0],
                name: "file",
                formData: null,
                success: function(t) {
                    var a = JSON.parse(t.data);
                    a.data.img_url, e.data.msgText = a.data.img_url, e.sendMsg(1);
                },
                complete: function(a) {
                    wx.hideLoading(), t.tempFilePaths.shift(), e.unload(t);
                }
            });
        }
    },
    init: function() {
        var t = this;
        if (this.data.plugin_name && "lawyer" == this.data.plugin_name) {
            var e = {
                message_type: this.data.message_type,
                employee_id: this.data.employeeId
            };
            return this.data.userUid && this.data.queue_id && (e.user_uid = this.data.userUid, 
            e.queue_id = this.data.queue_id), n._postNetWork({
                url: n.getNetAddresss("plugin.lawyer-platform.frontend.controllers.message.index"),
                data: e,
                success: function(e) {
                    var a = e.data;
                    if (1 !== a.result) return t.setData({
                        disabledSend: !0
                    }), n.tips(a.msg);
                    if (!a.data.employee_info) return n.tips("客服都不在线");
                    for (var s = a.data.emoji_list, i = {}, o = 0; o < s.length; o++) i[s[o].name] = s[o];
                    if (t.setData({
                        emojiList: s,
                        emojiObjs: i,
                        employeeInfo: a.data.employee_info,
                        userUid: a.data.member_info.uid,
                        memberInfo: a.data.member_info,
                        employeeId: a.data.employee_info.id
                    }), a.data.queue_info && t.setData({
                        queue_id: a.data.queue_info.id
                    }), a.data.queue_list) {
                        var r = a.data.queue_list.filter(function(e) {
                            return e.uid == t.data.userUid;
                        })[0];
                        r && r.id && t.setData({
                            queue_id: r.id
                        });
                    }
                    wx.setNavigationBarTitle({
                        title: 2 == t.data.chatType ? t.data.memberInfo.nickname : t.data.employeeInfo.nickname
                    }), t.initWebSocket(), t.getChatList();
                }
            }), !1;
        }
        var a = n.getNetAddresss("plugin.yun-chat.frontend.h5.chat.index");
        2 == this.data.chatType && (a = n.getNetAddresss("plugin.yun-chat.frontend.chat.index")), 
        console.log(this.data.groupId), console.log(this.data.employeeId), console.log(this.data.userUid), 
        n._postNetWork({
            url: a,
            data: {
                group_id: this.data.groupId,
                employee_id: this.data.employeeId,
                user_uid: this.data.userUid
            },
            success: function(e) {
                var a = e.data;
                if (console.log(a.data.wechatkf_url), 1 != a.data.no_permission) {
                    if (a.data.wechatkf_url && t.setData({
                        URL: a.data.wechatkf_url
                    }), 1 !== a.result) return t.setData({
                        disabledSend: !0
                    }), n.tips(a.msg);
                    if (a.data.corpid && a.data.url && t.setData({
                        customerPopupShow: !0,
                        cordId: a.data.corpid,
                        wx_url: a.data.url
                    }), a.data.emoji_list) {
                        for (var s = a.data.emoji_list, i = {}, o = 0; o < s.length; o++) i[s[o].name] = s[o];
                        t.setData({
                            emojiObjs: i,
                            emojiList: s
                        });
                    }
                    if (a.data.employee_info) {
                        if (t.setData({
                            employeeInfo: a.data.employee_info,
                            userUid: a.data.member_info.uid,
                            memberInfo: a.data.member_info,
                            employeeId: a.data.employee_info.id
                        }), a.data.queue_info && t.setData({
                            queue_id: a.data.queue_info.id
                        }), a.data.queue_list) {
                            var r = a.data.queue_list.filter(function(e) {
                                return e.uid == t.data.userUid;
                            })[0];
                            r && r.id && t.setData({
                                queue_id: r.id
                            });
                        }
                        wx.setNavigationBarTitle({
                            title: 2 == t.data.chatType ? t.data.memberInfo.nickname : t.data.employeeInfo.nickname
                        }), t.initWebSocket(), t.getChatList();
                    }
                } else wx.showModal({
                    title: "",
                    content: a.data.condition_msg ? a.data.condition_msg : "您不具备咨询资格，请先升级",
                    showCancel: !1,
                    complete: function(t) {
                        t.confirm && wx.redirectTo({
                            url: a.data.condition_jump_min ? a.data.condition_jump_min : "/packageG/index/index"
                        });
                    }
                });
            }
        });
    },
    customerPopupShow: function() {
        this.setData({
            customerPopupShow: !1
        });
    },
    jumpCustomer: function() {
        wx.openCustomerServiceChat({
            corpId: this.data.cordId,
            extInfo: {
                url: this.data.wx_url
            }
        });
    },
    getChatList: function() {
        var t = this;
        if (!this.data.chatGetLoading && !this.data.chatFinished) {
            this.setData({
                chatGetLoading: !0
            });
            var e = n.getNetAddresss("plugin.yun-chat.frontend.h5.chat.chats");
            2 == this.data.chatType && (e = n.getNetAddresss("plugin.yun-chat.frontend.chat.chats"));
            var a = {
                page: this.data.chatLoadedPage,
                queue_id: this.data.queue_id
            };
            this.data.plugin_name && "lawyer" == this.data.plugin_name && (e = n.getNetAddresss("plugin.lawyer-platform.frontend.controllers.message.chatList"), 
            a.message_type = this.data.message_type, 3 == this.data.message_type && (a.customer_type = 1)), 
            n._postNetWork({
                url: e,
                data: a,
                success: function(e) {
                    t.setData({
                        chatGetLoading: !1
                    });
                    var a = e.data;
                    if (1 !== a.result) return n.tips(a.msg);
                    (a.data.last_page == a.data.current_page || a.data.data.length < a.data.per_page) && t.setData({
                        chatFinished: !0
                    });
                    var i = a.data.data ? JSON.parse(JSON.stringify(a.data.data)) : [];
                    if (i.length > 0) {
                        var o = t.data.chatList[0], r = [], d = [];
                        i.reduce(function(t, e, a, s) {
                            if (void 0 === t && (t = s[a - 1]), t) {
                                var i = c(e.created_at, t.created_at), o = i.minutes, n = i.hours, h = i.days;
                                (o > 5 || n > 1 || h > 1) && (r.push(a + r.length), d.push(a));
                            }
                        });
                        for (var u = 0; u < r.length; u++) {
                            var l = r[u];
                            i.splice(l, 0, {
                                _is_time: !0,
                                _message_time: h(a.data.data[d[u]].created_at, "h:i")
                            });
                        }
                        var p = t.data.chatList;
                        if (p.unshift.apply(p, s(t.fmtChatList(i))), o && i.length > 0) {
                            var g = c(o.created_at, i[i.length - 1].created_at), m = g.hours, f = g.minutes, _ = g.days;
                            (f > 5 || m > 1 || _ > 1) && p.unshift({
                                _is_time: !0,
                                _message_time: h(a.data.data[0].created_at, "y年m月d日 h:i:s")
                            });
                        }
                        if (1 == t.data.chatLoadedPage) {
                            var y = "listItem" + (p.length - 1);
                            t.setData({
                                chatList: p
                            }, function() {
                                setTimeout(function() {
                                    t.setData({
                                        scrollTopTarget: y
                                    }, function() {
                                        t.setData({
                                            scrollTopAnimation: !0
                                        });
                                    });
                                }, 100);
                            });
                        } else {
                            t.setData({
                                scrollTopAnimation: !1
                            });
                            var w = "listItem" + (i.length - 1);
                            t.setData({
                                scrollTopTarget: w,
                                chatList: p
                            }, function() {});
                        }
                    }
                    t.data.chatLoadedPage++;
                }
            });
        }
    },
    selectEmoji: function(t) {
        var e = t.currentTarget.dataset.item, a = e.text, s = this.data.msgText, i = this.data.operation.iptCursor;
        this.setData({
            msgText: s.substring(0, i) + a + s.substring(i, s.length),
            "operation.emotion": !1
        }), this.data.operation.iptCursor += a.length, this.data.emojisTmp.push(e);
    },
    scrollBottom: function() {
        var t = this;
        console.log("执行"), this.setData({
            scrollTopAnimation: !0
        }), setTimeout(function() {
            console.log("延迟滚动"), "listItemBottom1" == t.data.scrollTopTarget ? t.setData({
                scrollTopTarget: "listItemBottom2"
            }) : t.setData({
                scrollTopTarget: "listItemBottom1"
            });
        }, 400);
    },
    handInput: function(t) {
        console.log("输入时触发", t);
    },
    handPageTouchStart: function() {
        console.log("zhix handPageTouchStart"), this.floatDeFocus();
        var t = this.data.operation;
        t.flag && (wx.hideKeyboard(), this.setData({
            "operation.flag": !1,
            "operation.height": t.dropHeight
        }));
    },
    getNowDateTime: function() {
        var t = new Date(), e = t.getMonth() + 1, a = t.getDate();
        return e >= 1 && e <= 9 && (e = "0" + e), a >= 0 && a <= 9 && (a = "0" + a), t.getFullYear() + "-" + e + "-" + a + " " + t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds();
    },
    fmtChatList: function(t) {
        if (t instanceof Array) for (var e = 0; e < t.length; e++) 2 == t[e].content_type || 3 == t[e].content_type ? t[e].content = JSON.parse(decodeURIComponent(t[e].content)) : 0 == t[e].content_type && (t[e].content = this.replaceEmoji(t[e].content)); else t instanceof Object && (2 == t.content_type || 3 == t.content_type ? t.content = JSON.parse(decodeURIComponent(t.content)) : 0 == t.content_type && (t.content = this.replaceEmoji(t.content)));
        return t;
    },
    getGoodsList: function() {
        var t = this, e = n.getNetAddresss("plugin.yun-chat.frontend.goods.queryList");
        n._postNetWork({
            url: e,
            data: {
                kwd: this.data.goodsSearchKwd
            },
            success: function(e) {
                var a = e.data;
                if (1 !== a.result) return n.tips(a.msg);
                t.setData({
                    goodsList: a.data
                });
            }
        });
    },
    selectGoods: function(t) {
        var e = t.currentTarget.dataset.item, a = {};
        a.id = e.id, a.title = e.title, a.thumb = e.thumb, a.price = e.min_price || e.price, 
        console.log(JSON.stringify(a)), this.data.msgText = encodeURIComponent(JSON.stringify(a)), 
        console.log(this.msgText), this.sendMsg(2), this.setGoodsPopupShow();
    },
    setGoodsPopupShow: function() {
        this.setData({
            goodsPopupShow: !this.data.goodsPopupShow
        });
    },
    getOrderList: function() {
        var t = this, e = "", a = {};
        1 == this.data.chatType ? (e = n.getNetAddresss("order.list"), a = {
            keyword: this.data.orderSearchKeyword
        }) : 2 == this.data.chatType && (e = n.getNetAddresss("plugin.yun-chat.frontend.order-list.get-data"), 
        a = {
            user_uid: this.data.userUid,
            order_sn: this.data.orderSearchKeyword
        }), n._postNetWork({
            url: e,
            data: a,
            success: function(e) {
                var a, s = e.data;
                if (1 !== s.result) return n.tips(s.msg);
                1 == t.data.chatType ? a = s.data.data : 2 == t.data.chatType && (a = s.data), t.setData({
                    orderList: a
                });
            }
        });
    },
    selectOrder: function(t) {
        var e = t.currentTarget.dataset.item;
        this.data.msgText = encodeURIComponent(JSON.stringify(e)), this.sendMsg(3), this.setOrderPopupShow();
    },
    setOrderPopupShow: function() {
        this.setData({
            orderPopupShow: !this.data.orderPopupShow
        });
    },
    getReplyList: function() {
        var t = this, e = n.getNetAddresss("plugin.yun-chat.frontend.common-reply.query");
        n._postNetWork({
            url: e,
            data: {
                kwd: this.data.replySearchKwd
            },
            success: function(e) {
                var a = e.data;
                if (1 !== a.result) return n.tips(a.msg);
                t.setData({
                    replyList: a.data
                });
            }
        });
    },
    selectReply: function(t) {
        var e = t.currentTarget.dataset.item;
        this.data.msgText = e.content, this.sendMsg(0), this.setReplyPopupShow();
    },
    setReplyPopupShow: function() {
        this.setData({
            replyPopupShow: !this.data.replyPopupShow
        });
    },
    getTransferEmployeeList: function() {
        var t = this, e = n.getNetAddresss("plugin.yun-chat.frontend.employee.queryList");
        n._postNetWork({
            url: e,
            data: {
                kwd: this.data.transferSearchKwd
            },
            success: function(e) {
                var a = e.data;
                if (1 !== a.result) return n.tips(a.msg);
                t.setData({
                    transferEmployeeList: a.data
                });
            }
        });
    },
    selectTransfer: function(t) {
        var s = this;
        return a(e.default.mark(function a() {
            var i, o;
            return e.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return i = t.currentTarget.dataset.item, e.next = 3, n.confirm("确定转接给（".concat(i.nickname, "）对接？"));

                  case 3:
                    if (e.sent) {
                        e.next = 6;
                        break;
                    }
                    return e.abrupt("return");

                  case 6:
                    o = n.getNetAddresss("plugin.yun-chat.frontend.chat.transfer"), n._postNetWork({
                        url: o,
                        data: {
                            employee_id: i.id,
                            user_uid: s.data.userUid
                        },
                        success: function(t) {
                            var e = t.data;
                            if (1 !== e.result) return n.tips(e.msg);
                            wx.navigateBack();
                        }
                    });

                  case 8:
                  case "end":
                    return e.stop();
                }
            }, a);
        }))();
    },
    setTransferPopupShow: function() {
        this.setData({
            transferPopupShow: !this.data.transferPopupShow
        });
    },
    toCustomerServicePage: function() {
        try {
            wx.openCustomerServiceChat({
                corpId: "ww5855fd777b6e2fc2",
                extInfo: {
                    url: this.data.URL
                },
                success: function(t) {}
            });
        } catch (t) {
            console.log(t, "*****************");
        }
    },
    initWebSocket: function() {
        var t = this;
        console.log("执行---initWebSocket"), this.setData({
            msg_id_list: []
        }), this.clearHeartBeat();
        var e = n.getNetAddresss("plugin.yun-chat.frontend.h5.chat.get-ws-setting");
        2 == this.data.chatType && (e = n.getNetAddresss("plugin.yun-chat.frontend.chat.get-ws-setting")), 
        e += e + "&employee_id=" + this.data.employeeId, n._getNetWork({
            url: e,
            success: function(e) {
                var a = e.data;
                if (1 !== a.result) return n.tips(a.msg);
                t.setData({
                    chat_opt_setting: a.data.chat_opt_setting,
                    show_tool_bar: a.data.show_tool_bar || 0,
                    chat_tool_bars: a.data.chat_tool_bars || []
                });
                var s = a.data.wss_params, i = s.host + "?appId=" + s.appId + "&signature=" + s.signature + "&nonceStr=" + s.nonceStr + "&timestamp=" + s.timestamp + "&clientid=" + s.clientid + "&agent_id=" + s.agent_id;
                t.data.websock = wx.connectSocket({
                    url: i
                }), t.data.websock.onMessage(function(e) {
                    t.websocketonmessage(e);
                }), t.data.websock.onOpen(function() {
                    t.websocketonopen();
                }), t.data.websock.onError(function() {
                    t.websocketonerror();
                }), t.data.websock.onClose(function(e) {
                    t.websocketclose(e);
                });
            }
        });
    },
    websocketonmessage: function(t) {
        console.log("message", t);
        var e = JSON.parse(t.data);
        if (e.msg_id) {
            if (this.data.websock.send({
                data: JSON.stringify({
                    type: "msg_ack",
                    msg_id: e.msg_id
                })
            }), this.data.msg_id_list.indexOf(e.msg_id) > -1) return;
            this.data.msg_id_list.push(e.msg_id);
        }
        var a = [];
        switch (e.type) {
          case "init":
            return;

          case "text":
            if (1 == this.data.chatType && this.data.employeeId == e.employee_id || 2 == this.data.chatType && this.data.userUid == e.uid) {
                var s, i = e.direction_type ? e.direction_type : 0;
                (1 == this.data.chatType || 2 == this.data.chatType) && (s = this.generateChatData(e.data, i, e.time, e.content_type)), 
                s.id = parseInt(e.id);
                for (var o = 0; o < this.data.chatList.length; o++) a.push(this.data.chatList[o]);
                a.push(s), this.setData({
                    chatList: a
                });
            } else e.employee_id != this.data.employee_id ? (this.data.websock && this.data.websock.close(), 
            this.data.employeeId = e.employee_id, this.data.chatList = [], this.data.chatFinished = !1, 
            this.data.chatLoading = !1, this.data.chatLoadedPage = 1, this.init()) : n.tips("转接失败");
            if (1 == this.data.chatType) {
                var r = n.getNetAddresss("plugin.yun-chat.frontend.h5.chat.is-read");
                n._postNetWork({
                    url: r,
                    data: {
                        chats_id: e.id
                    },
                    success: function() {}
                });
            } else {
                var d = n.getNetAddresss("plugin.yun-chat.frontend.chat.is-read");
                n._postNetWork({
                    url: d,
                    data: {
                        chats_id: e.id
                    },
                    success: function() {}
                });
            }
            return void this.scrollBottom();

          case "backed":
            for (var c = 0; c < this.data.chatList.length; c++) this.data.chatList[c].id == e.data && (this.data.chatList[c].is_backed = 1), 
            a.push(this.data.chatList[c]);
            return this.setData({
                chatList: a
            }), void this.scrollBottom();
        }
    },
    websocketonopen: function() {
        console.log("链接打开"), this.heartBeatStart();
    },
    websocketonerror: function() {
        this.clearHeartBeat(), this.data.isClosed || this.reconnect();
    },
    websocketclose: function(t) {
        console.log("断开连接", t), this.clearHeartBeat(), this.data.isClosed || this.reconnect();
    },
    heartBeatStart: function() {
        var t = this;
        this.data.timeoutObj && clearTimeout(this.data.timeoutObj), this.data.serverTimeoutObj && clearTimeout(this.data.serverTimeoutObj), 
        this.data.timeoutObj = setTimeout(function() {
            console.log(t.data.websock, t.data.websock.readyState, "websock"), 1 == t.data.websock.readyState ? (t.data.websock.send({
                data: JSON.stringify({
                    type: "ping"
                })
            }), t.keepHeartBeat(), t.data.currentReconnectNum = 0) : t.reconnect();
        }, this.data.timeout);
    },
    clearHeartBeat: function() {
        clearInterval(this.data.heartBeatInterval), this.data.heartBeatInterval = null;
    },
    keepHeartBeat: function() {
        var t = this;
        this.data.heartBeatInterval = setInterval(function() {
            t.data.websock.send({
                data: JSON.stringify({
                    type: "ping"
                })
            });
        }, 6e4);
    },
    reconnect: function() {
        var t = this;
        this.data.lockReconnect || this.data.isClosed || this.data.currentReconnectNum >= this.data.maxReconnectNum ? this.data.websock && this.data.websock.close() : (this.data.lockReconnect = !0, 
        this.data.timeoutnum && clearTimeout(this.data.timeoutnum), this.data.timeoutnum = setTimeout(function() {
            console.log(t.data.currentReconnectNum), t.data.currentReconnectNum += 1, t.initWebSocket(), 
            t.data.lockReconnect = !1;
        }, 5e3));
    },
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});